package com.andatsoft.laisim.dialog;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.andatsoft.laisim.R;
import com.andatsoft.laisim.activity.BaseActivity;
import com.andatsoft.laisim.utils.Common;

public class FileExtensionDialog extends BaseActivity {

	private TextView mTvOk;
	private CheckBox[] mExtChkBox = new CheckBox[9];

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		getWindow().setBackgroundDrawable(
				new ColorDrawable(getResources().getColor(R.color.black)));
		setContentView(R.layout.dialog_file_ext);
		initViews();
		setupListeners();
	}

	private void initViews() {
		mTvOk = (TextView) findViewById(R.id.tv_ok_setting_file_ext);
		mExtChkBox[0] = (CheckBox) findViewById(R.id.chk_box_file_ext_mp3);
		mExtChkBox[1] = (CheckBox) findViewById(R.id.chk_box_file_ext_flac);
		mExtChkBox[2] = (CheckBox) findViewById(R.id.chk_box_file_ext_wav);
		mExtChkBox[3] = (CheckBox) findViewById(R.id.chk_box_file_ext_mp4);
		mExtChkBox[4] = (CheckBox) findViewById(R.id.chk_box_file_ext_m4a);
		mExtChkBox[5] = (CheckBox) findViewById(R.id.chk_box_file_ext_aac);
		mExtChkBox[6] = (CheckBox) findViewById(R.id.chk_box_file_ext_ogg);
		mExtChkBox[7] = (CheckBox) findViewById(R.id.chk_box_file_ext_mid);
		mExtChkBox[8] = (CheckBox) findViewById(R.id.chk_box_file_ext_3gp);

		restoreUIFromSetting();
	}

	private void restoreUIFromSetting() {
		SharedPreferences sp = getSharedPreferences(Common.SP_LIB_SETTING_NAME,
				Context.MODE_PRIVATE);
		String str = sp.getString(Common.SP_LIB_FILE_EXT, "111000000");
		restoreExtVals(str);
	}

	private void restoreExtVals(String src) {
		for (int i = 0; i < src.length(); i++) {
			int val = src.charAt(i) - 48;
			if (val == 1) {
				mExtChkBox[i].setChecked(true);
			}
		}
	}

	private void setupListeners() {
		mTvOk.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					setAlpha(v, 0.5f);
					return true;

				case MotionEvent.ACTION_UP:
					setAlpha(v, 1.0f);
					saveSetting();
					FileExtensionDialog.this.finish();
					return true;
				}
				return false;
			}
		});

	}

	private void saveSetting() {
		StringBuilder vals = new StringBuilder();
		for (CheckBox chk : mExtChkBox) {
			if (chk.isChecked()) {
				vals.append(1);
			} else {
				vals.append(0);
			}
		}
		// save setting value
		SharedPreferences sp = getSharedPreferences(Common.SP_LIB_SETTING_NAME,
				Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sp.edit();
		editor.putString(Common.SP_LIB_FILE_EXT, vals.toString());
		editor.commit();
	}
}
